import './assets/main.css'
import { createApp } from 'vue'
import {ElButton, ElContainer, ElAside, ElMain, ElTabs, ElIcon, ElImage, ElMenu, ElStatistic, ElInput, ElRow, ElCol, ElAlert, ElMessageBox, ElMessage, ElForm, ElUpload} from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
import router from './router'
import mitt from 'mitt'
import mqtt from 'mqtt'
import vue3TreeOrg from 'vue3-tree-org';
import "vue3-tree-org/lib/vue3-tree-org.css";

const app = createApp(App)

app.use(ElButton).use(ElContainer).use(ElAside).use(ElMain).use(ElTabs).use(ElIcon).use(ElImage).use(ElMenu).use(ElStatistic).use(ElInput).use(ElRow).use(ElCol).use(ElAlert).use(ElMessageBox).use(ElMessage).use(ElForm).use(ElUpload)
app.use(vue3TreeOrg)
app.use(router)


const emitter=mitt()
app.provide('mitt',emitter)

const options={
clean:true, 
connectTimeout:4000, 
clientId:new Date().getTime().toString()+'|'+Math.ceil(Math.random()*1000)
}
const url='ws://robot.industrybrain-cq.cn:8083/mqtt'
const client=mqtt.connect(url,options)
client.on('reconnect', error => {
console.error('MQTT正在重连:', error)
})
client.on('error', error => {
console.error('MQTT连接失败:', error)
})
//收到消息
/* 
topics:对应不同权限
    /any                                    任何人（包括游客）
    /user/${user_id}                        所有用户
    /organization/${organization_id}        机构所有用户
    /organization_admin/${organization_id}  机构管理员
    /repoter                                媒体专员
    /auditor                                审核员
    
message:
    事件名（变量名）
*/
client.on('message', (topic, message) => {
    console.log('MQTT收到消息：', topic, message.toString()) //message是二进制流，需要转换成字符串
    var event=message.toString()
    emitter.emit(event)
})

app.provide('mqtt',client)



app.mount('#app')

